# -*- coding: utf-8 -*-
_author_ = 'chenchuchuan'

import time
import urlparse

def format_stamp_local_time(val):
    """
    时间戳格式化
    :param val:
    :return:
    """
    x = time.localtime(val)
    return time.strftime('%Y-%m-%d %H:%M:%S', x)

def format_datetime_to_str(current_time):
    format_time = current_time.strftime("%Y-%m-%d %H:%M:%S")

    return format_time


def convert_to_dict(obj):
    '''把Object对象转换成Dict对象'''
    if obj is None:
        return {}
    dict_ = {}
    obj._sa_instance_state = ''
    dict_.update(obj.__dict__)
    try:
        dict_.__delitem__("_sa_instance_state")
    except:
        pass
    for i in dir(obj):  # 把自定义图片地址加入字典
        if i.startswith('created_'):
            dict_.update({i: eval("str(obj.%s)" % i)})
    return dict_


def obj_to_dict(data_list):
    """将model-list 对象转换为字典"""
    data = []
    for c in data_list:
        data.append(convert_to_dict(c))
    return data


def raw_unicode(data):
    if isinstance(data,unicode):
        try :
            return data.encode('raw_unicode_escape')
        except:
            return data
    return data


def get_param_for_url(url):
    '''
    print qs('http://url/api?param=2&param2=4') 返回的结果:{'param':'2','param2':'4'}
    :param url:
    :return:
    '''
    query = urlparse.urlparse(url).query
    return dict([(k,v[0]) for k,v in urlparse.parse_qs(query).items()])

